/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.server;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author mark
 */
public class ServerScreen extends JFrame implements ActionListener {
    
    private Main main;
    
    private String dateTime;
    
    private JTextArea logtext;
    private JPanel panel;
    private String log = "";
    
    private JMenuBar menubar;
    private JMenu menu_options;
    private JMenuItem menuitem_start;
    private JMenuItem menuitem_stop;
    
    private JScrollPane scrollpane;
    
    public ServerScreen(Main main)
    {
        this.main = main;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        panel = new JPanel();
        setSize(640, 480);
        
        logtext = new JTextArea(33, 55);
        
        menubar = new JMenuBar();
        menu_options = new JMenu("Options");
        
        menuitem_start = new JMenuItem("Start");
        menuitem_stop = new JMenuItem("Stop");
        
        scrollpane = new JScrollPane(logtext);        
        //scrollpane.setPreferredSize(new Dimension(620, 410));
        
        panel.add(scrollpane, BorderLayout.CENTER);
        panel.add(menubar);
        menubar.add(menu_options);
        
        menu_options.add(menuitem_start);
        menu_options.add(menuitem_stop);
        
        menuitem_start.addActionListener(this);
        menuitem_stop.addActionListener(this);
        
        setTitle("Info Server");
        setContentPane( panel );
        setJMenuBar( menubar );
    }
    
    public void addToLog(String add)
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        this.dateTime = dateFormat.format(date);
        log = log += dateTime + " " + add + "\n";
        refreshLogTekst(log);
    }
    
    public void refreshLogTekst(String log)
    {
        logtext.setText(log);
    }
    
    public void actionPerformed(ActionEvent e) 
    {
        if( e.getSource() == menuitem_start )
        {
            logtext.setBackground(Color.GREEN);
            //SocketConnection sc = new SocketConnection(main);
            //sc.handleConnection();
        }
        else if( e.getSource() == menuitem_stop )
        {
            logtext.setBackground(Color.RED);
        } 
    }
    
}
